﻿using Barcode_Project.Entity;
using Barcode_Project.Local;
using Barcode_Project.Network;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Barcode_Project.InitPrint
{
    public partial class VendorSelector : DevExpress.XtraEditors.XtraForm
    {
        public Filteredvendor selectedVendor { get; set; }

        public Dictionary<string, string> selectedVen = new Dictionary<string, string>();

        public VendorSelector()
        {
            InitializeComponent();

            SearchVendor();

        }
        // 搜索供应商
        void SearchVendor()
        {
            var dic = new Dictionary<string, string>();
            CommonMethods.SetParameterValue(ref dic, group_data.Controls, "input_");

            var data = new RemoteRequest().SearchVendors(dic);

            gridControl_vendors.DataSource = data;
            gridControl_vendors.RefreshDataSource();
        }

        private void button_search_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SearchVendor();
        }

        private void button_confirm_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;

            var gridView = gridControl_vendors.FocusedView as GridView;

            var item = (VendorSelectItem)gridView_vendors.GetRow(gridView.FocusedRowHandle);

            selectedVendor = new Filteredvendor
            {
                cuCode = item.cuCode,
                cuName = item.cuName,
            };

            selectedVen["cuCode"] = selectedVendor.cuCode;
            selectedVen["cuName"] = selectedVendor.cuName;

            Close();
        }


        private void gridView_products_DoubleClick(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;

            var gridView = gridControl_vendors.FocusedView as GridView;

            var item = (VendorSelectItem)gridView_vendors.GetRow(gridView.FocusedRowHandle);

            selectedVendor = new Filteredvendor
            {
                cuCode = item.cuCode,
                cuName = item.cuName,
            };

            selectedVen["cuCode"] = selectedVendor.cuCode;
            selectedVen["cuName"] = selectedVendor.cuName;

            Close();
        }

        private void btn_close_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void input_cuCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SearchVendor();
            }
        }

        private void input_cuName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SearchVendor();
            }
        }



    }
}